<?php
require_once 'index.php';

$mScheduler = new Application_Model_DbTable_Scheduler();

$schedulers = $mScheduler->findSchedulers();

if(! empty($schedulers))
{
	$mQueue = new Application_Model_DbTable_Queue();
	
	$mList = new Application_Model_DbTable_List();
	
	$mContact = new Application_Model_DbTable_Contact();
	
	$mCampaign = new Application_Model_DbTable_Campaign();
	
	$mSmtp = new Application_Model_DbTable_Smtp();
	
	foreach ($schedulers as $scheduler)
	{
		$smtp = $mSmtp->getSmtp($scheduler['smtp_id']);
		
		$listIds = $scheduler['list_ids'];
		
		$listIds = explode(',', $listIds);
		
		$campaign = $mCampaign->getCampaign($scheduler['campaign_id']);
		
		$title = $campaign['title'];
		
		$body = $campaign['body'];
		
		foreach ($listIds as $listId)
		{
			$list = $mList->getList($listId);
			
			$owner = $list['owner_name'];
			
			$ownerEmail = $list['owner_email'];
			
			$replyEmail = $list['reply_email'];
			
			$contacts = $mContact->findContacts($listId);
			
			foreach ($contacts as $contact)
			{
				$email = $contact['email'];
		
				$mail = Application_Helper_Mail::getInstance();
				
				$mail->connect($smtp['server'], $smtp['port'], $smtp['username'], $smtp['password'], $smtp['tls']);
				
				Application_Helper_Mail::send($ownerEmail, $email, $replyEmail, $title, $body);
			}
		}
		// list_ids
	}
}